#!/bin/bash

alg=$1
step=$2

touch ../trafficAgents/dcop/common/dcop_output.txt

time=""
cycles=""
msgs=""
avgmsgsize=""

# parse lines
processLine(){
  	line="$@" # get all args
  	
	name=`echo $line | awk -F= '{print $1}'`
	value=`echo $line | awk -F= '{print $2}'`

	if [ $name == "TOTALTIME" ]
	then
		time=$value
	fi

	if [ $name == "CYCLES" ] 
	then
		cycles=$value
	fi

	if [ $name == "MSGS" ] 
	then
		msgs=$value
	fi

	if [ $name == "AVGMSGSIZE" ] 
	then
		avgmsgsize=$value
	fi
}


# read $FILE using the file descriptors
processFile(){
	ff="$@"
	exec 3<&0
	exec 0<$ff

	while read line
	do
		# use $line variable to process line in processLine() function
		processLine $line
	done
	exec 0<&3
	
	pwd
	echo $step $time $cycles $msgs $avgmsgsize >> ../trafficAgents/dcop/common/dcop_output.txt

}

if [ $1 == 1 ]
then
	echo "ADOPT !!!"
	pwd
	cd ../trafficAgents/dcop/adopt/modi_adopt/novo/preAdopt/adopt/solver/

	rm -rf Logs/*
	cp ../../../../../../common/adopt.txt ./

	java -classpath ../../ adopt.solver.Simulator adopt maxcop-tree adopt.txt 2 0

	cp Logs/Master.log ../../../../../../common/adopt_result_$2.txt
	cat Logs/Summary.txt >> ../../../../../../common/adopt_result_$2.txt

	cd -	

	ff=../trafficAgents/dcop/common/adopt_result_$2.txt

	processFile $ff

	
fi

if [ $1 == 2 ]
then
	echo "DPOP !!!"
	cd ../trafficAgents/dcop/dpop/frodo/

	rm -rf dpop_result.txt

	java -Xmx512000K -classpath ./classes:./libs/jgraph.jar:./libs/jgraphaddons.jar frodo.OCOP.DPOP.TesterDPOP ../../common/dpop.txt dpop_result.txt 0

	mv dpop_result.txt ../../common/dpop_result_$2.txt

	cd -

	ff=../trafficAgents/dcop/common/dpop_result_$2.txt

	processFile $ff
fi

if [ $1 == 3 ]
then
	echo "OptAPO !!!"
	cd ../trafficAgents/dcop/optapo/optapo/

	rm -rf optapo_result.txt

	java -classpath ../ optapo.simulator ../../common/optapo.txt o

	mv optapo.txt ../../common/optapo_result_$2.txt

	cd -

	ff=../trafficAgents/dcop/common/optapo_result_$2.txt

	processFile $ff

fi
